package com.roy.wordcount;

import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

import java.util.Map;

public class WordCountSpout extends BaseRichSpout {
    SpoutOutputCollector collector;

    //初始化方法
    public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector collector) {
        //主要的步骤：将框架的collector装进自己的成员变量，然后用collector发送tuple。
        this.collector = collector;
    }
    //storm会在while true中循环调用nextTuple
    public void nextTuple() {
        collector.emit(new Values("test word count Storm word count as hadoop and java edition"));
    }
    //指定输出类型

    /**
     * 这个fileds应该是个values对应的。相当于给每个字段定义了一个列名。
     * 这样，在下一个Bolt中，可以用tuple.get***(index)或者tuple.get***ByFields(field)来拿到value.
     * @param outputFieldsDeclarer
     */
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        //声明输出字段
        outputFieldsDeclarer.declare(new Fields("wordCount"));
    }
}
